import ctypes
import os
import sys
import time
from configparser import ConfigParser

from common.log.log import log

"""
cmd执行功能模块
"""


def readConf(option, section='conf'):
    filename = r"E:/path.ini"
    config = ConfigParser()
    config.read(filename)
    content = config.get(section, option)
    return content


def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False


# 断开网络连接
def eth_break():
    if is_admin():
        cmd = "netsh interface set interface 以太网 disabled"
        os.popen(cmd)
        time.sleep(2)
        log.info("禁用网络成功")
        print("禁用网络成功")
        time.sleep(5)
        return True
    if sys.version_info[0] == 3:
        print("没有管理员权限运行cmd命令")
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
        time.sleep(2)


def eth_connect1111():
    if is_admin():
        cmd = "netsh interface set interface 以太网 enabled"
        os.popen(cmd)
        while True:
            ip = readConf("ip")
            for i in os.popen(f"ping {ip}"):
                if "传输失败" not in i:
                    log.info("启用网络成功")
                    print("启用网络成功")
                    return True
            continue
    if sys.version_info[0] == 3:
        print("没有管理员权限运行cmd命令")
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
